package com.example.easypad;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class activiti_new_Edit extends AppCompatActivity {
    private String type = "生活";
    private Spinner sp_type;
    private ImageButton ib_back;
    private EditText et_context;
    private EditText et_title;
    String context,title;
    private Button btn;
    private TextView test;
    private EditText et_username, et_pwd;
    private List<Notes> n;
//    private SQLiteOpenHelper helper = null;
    private SQLiteDatabase db;
    private String username, password, sql = "";
    private Cursor cursor = null;
    private boolean isExist = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activiti_new_edit);
        db = MainActivity.db;
        initLayout();

        sp_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//                Toast.makeText(activiti_new_Edit.this,position + "",Toast.LENGTH_SHORT).show();
                switch (position){
                    case 0 : type = "生活";
                    break;
                    case 1 : type = "工作";
                    break;
                    case 2 : type = "娱乐";
                    break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = et_context.getText() + "";
                if(text.equals("")) {
                    finish();
                    return;
                }
                sql = "insert into Notes (title,context,type,dateTime,username) values ('" + et_title.getText() + "', '" + et_context.getText() + "', '" + type + "', '" + MainActivity.getDateTime() + "', '" + MainActivity.userName + "')";
                db.execSQL(sql);
                MainActivity.showData();
                MainActivity.myAdapter = new MyAdapter(MainActivity.data,MainActivity.mainContext);
                MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);
//        saveData();
                finish();
            }
        });



    }
    private void saveData() {
        //MainActivity.data.add(new Notes(getTextViewTitle(),getTextViewContext(),"测试类型1"));
        //MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);
    }

    @Override
    public void onBackPressed() {
        context = et_context.getText() + "";
        title = et_title.getText() + "";
        if(context.equals("") || et_title.equals("")) {
            finish();
            return;
        }
        sql = "insert into Notes (title,context,type,dateTime,username) values ('" + et_title.getText() + "', '" + et_context.getText() + "', '" + type + "', '" + MainActivity.getDateTime() + "', '" + MainActivity.userName + "')";
        db.execSQL(sql);
        MainActivity.showData();
        MainActivity.myAdapter = new MyAdapter(MainActivity.data,MainActivity.mainContext);
        MainActivity.lv_notes.setAdapter(MainActivity.myAdapter);
//        saveData();
        finish();
    }

    private void initSpinner() {
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_type.setAdapter(adapter);
        sp_type.setSelection(0);
    }

    private void initLayout() {
        ib_back = findViewById(R.id.ib_back);
        et_title = findViewById(R.id.et_title);
        et_context = findViewById(R.id.et_context);
        sp_type = findViewById(R.id.sp_type);
        initSpinner();
    }
}